home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 5 / Gold Medal Software - Volume 5 (Gold Medal) (1995).iso / database / cff51b.arj / COPYTEST.C < prev    next >
C/C++ Source or Header  |  1993-10-22  |  9KB  |  356 lines

  1. /* COPYEST.C */
  2. /* Copyright 1990, 1991, 1992, 1993 Norman D. Culver Ft. Lauderdale, FL */
  3. /*                    All Rights Reserved                                   */
  4.  
  5.  
  6. #include <stdlib.h>
  7. #include "../cff.h"
  8.  
  9. static void test1(void);
  10. static void test2(void);
  11. static void test3(void);
  12.  
  13. void
  14. main()
  15. {
  16.  
  17.     cfport_settestflags(1);
  18.     cfinit("copytest",512,NULL);
  19.  
  20.     cfprintf("TEST THE COPY FUNCTION\n");
  21.  
  22.     test1();
  23.     test2();
  24.     test3();
  25.  
  26.     cfexit();
  27. }
  28. static void
  29. test1()
  30. {
  31. void *h1, *h2, *h3;
  32. long i, err;
  33. long buf[50000];
  34.  
  35.     cfprintf("  Begin test1  -- file property\n");
  36.     if((h1 = cfopen("EXTDMEM/testfile", F_RDWR|F_CREAT|F_TEMP, NULL)) == NULL)
  37.     {
  38.         cfprintf("Failed to open first testfile\n");
  39.         return;
  40.     }
  41.  
  42.     for(i = 0; i < 50000; ++i)
  43.         buf[i] = i;
  44.  
  45.     if((err = cfwrite(h1, buf, sizeof(buf))) != sizeof(buf))
  46.     {
  47.         cfprintf("First buffer failed to write err=%d\n", err);
  48.         cfclose(h1);
  49.         return;
  50.     }
  51.  
  52.     if((h2 = cfcopy("EXTDMEM/newfile", h1)) == NULL)
  53.     {
  54.         cfprintf("Object copy #1 failed\n");
  55.         cfclose(h1);
  56.         return;
  57.     }
  58.     cfclose(h1);
  59.  
  60.     cfseek(h2, 0, S_SET);
  61.     for(i = 0; i < 50000; ++i)
  62.     {
  63.     long result;
  64.         if((err = cfread(h2, &result, sizeof(long))) != sizeof(long))
  65.         {
  66.             cfprintf("Read #1 failed at i=%d err=%d\n", i, err);
  67.             break;
  68.         }
  69.         else if(result != i) {
  70.             cfprintf("File read #1 failed at i=%d result=%d\n", i, result);
  71.             break;
  72.         }
  73.     }
  74.  
  75.     if((h1 = cfopen("tempfile.xxx", F_RDWR|F_CREAT|F_TEMP, NULL)) == NULL)
  76.     {
  77.         cfprintf("Failed to open tempfile.xxx\n");
  78.         cfunlink(h2, NULL);
  79.         return;
  80.     }
  81.  
  82.     for(i = 0; i < 50000; ++i)
  83.         buf[i] = 50000 - i;
  84.  
  85.     if((err = cfwrite(h1, buf, sizeof(buf))) != sizeof(buf))
  86.     {
  87.         cfprintf("Write #2 failed err=%d\n", err);
  88.         cfclose(h1);
  89.     }
  90.  
  91.  
  92.     if((h2 = cfcopy(h2, h1)) == NULL)
  93.     {
  94.         cfprintf("Failed to copy from tempfile.xxx to EXTDMEM/newfile\n");
  95.         cfclose(h1);
  96.         return;
  97.     }
  98.     cfclose(h1);
  99.  
  100.     cfseek(h2, 0, S_SET);
  101.     for(i = 50000; i > 0; --i)
  102.     {
  103.     long result;
  104.         if((err = cfread(h2, &result, sizeof(long))) != sizeof(long))
  105.         {
  106.             cfprintf("Read #2 failed at i=%d err=%d\n", i, err);
  107.             return;
  108.         }
  109.         if(result != i) {
  110.             cfprintf("File read #2 failed at i=%d result=%d\n", i, result);
  111.             break;
  112.         }
  113.     }
  114.  
  115.  
  116.     cfclose(cfcopy("testfile.cff", h2));
  117.     cfunlink(h2, NULL);
  118.  
  119.     if((h3 = cfopen("testfile.cff", F_RDWR, NULL)) == NULL)
  120.     {
  121.         cfprintf("Failed to open testfile.cff\n");
  122.         return;
  123.     }
  124.     for(i = 50000; i > 0; --i)
  125.     {
  126.     long result;
  127.         if((err = cfread(h3, &result, sizeof(long))) != sizeof(long))
  128.         {
  129.             cfprintf("Read #3 failed at i=%d\n", i);
  130.             break;
  131.         }
  132.         if(result != i) {
  133.             cfprintf("File read #3 failed at i=%d result=%d\n", i, result);
  134.             break;
  135.         }
  136.     }    
  137.  
  138.  
  139.     if((h1 = cfcopy("MEMORY/afile", h3)) == NULL)
  140.     {
  141.         cfprintf("Failed to copy to MEMORY/afile\n");
  142.         cfunlink(h3, NULL);
  143.         return;
  144.     }
  145.     for(i = 50000; i > 0; --i)
  146.     {
  147.     long result;
  148.         if((err = cfread(h1, &result, sizeof(long))) != sizeof(long))
  149.         {
  150.             cfprintf("Read #4 failed at i=%d\n", i);
  151.             break;
  152.         }
  153.         if(result != i) {
  154.             cfprintf("File read #4 failed at i=%d result=%d\n", i, result);
  155.             break;
  156.         }
  157.     }    
  158.     cfunlink(h3, NULL);
  159.     cfunlink(h1, NULL);
  160.     cfprintf("  End test1\n");
  161.  
  162. }
  163. static void
  164. test2()
  165. {
  166. void *h1, *h2, *h3, *h4;
  167. OPNINFO info;
  168. short buf[16];
  169. DupName dupname3, dupname4;
  170. int i,j;
  171. unsigned long alloc, used;
  172.  
  173.     cfprintf("  Begin test2 -- complex objects\n");
  174.     cftotalloc("EXTDMEM/", &used, &alloc);
  175.     cfprintf("  EXTDMEM bytesalloc=%d bytesused=%d\n", alloc*1000, used*1000);
  176.     if(!(h1 = cfopen("MEMORY/tfile", F_RDWR|F_CREAT|F_SORTED|F_TEMP, NULL)))
  177.     {
  178.         cfprintf("Failed to open sorted memory file.\n");
  179.         return;
  180.     }
  181.     info.initial_entries = 10000;
  182.     info.bitmap_prealloc = 32*10000;
  183.     info.data_prealloc = 32;
  184.     if(!(h2 = cfopen("MEMORY/hfile", F_RDWR|F_CREAT|F_BITMAP|F_TEMP, &info)))
  185.     {
  186.         cfprintf("Failed to open bitmapped memory file.\n");
  187.         return;
  188.     }
  189.     cfprintf("  Push data to hash and tree objects.\n");
  190.     for(i = 0; i < 20; ++i)
  191.     {
  192.         for(j = 0; j < 16; ++j)
  193.             buf[j] = i;
  194.         if(cfpush_data(h1, buf, 32) == ERROR)
  195.             cfprintf("Push h1 data failed at i=%d\n", i);
  196.         if(cfpush_data(h2, buf, 32) == ERROR)
  197.             cfprintf("Push h2 data failed at i=%d\n", i);
  198.     }
  199.     cfprintf("    Put a lot of data to hash and tree objects, using dupnames.\n");
  200.     for(i = 0; i < 3000; ++i)
  201.     {
  202.         for(j = 0; j < 16; ++j)
  203.             buf[j] = i;
  204.         if(cfput_dupname(h1, "address", 7, buf, 32, NULL, NULL) == NULL)
  205.             cfprintf("Put data to treedir failed at i=%d\n", i);
  206.         if(cfput_dupname(h2, "address", 7, buf, 32, NULL, NULL) == NULL)
  207.             cfprintf("Put data to hashdir failed at i=%d\n", i);
  208.     }
  209.     cfprintf("    Tree object alloc=%d used=%d.\n", cfbytesalloc(h1), cfbytesused(h1));
  210.     cfprintf("    Copy tree object to extended memory.\n");
  211.     if(!(h3 = cfcopy("EXTDMEM/tfile", h1))) {
  212.         cfprintf("Failed to copy tree object.\n");
  213.     }
  214.     cftotalloc("EXTDMEM/", &used, &alloc);
  215.     cfprintf("    EXTDMEM bytesalloc=%d bytesused=%d\n", alloc*1000, used*1000);
  216.     cfprintf("    Hash object alloc=%d used=%d.\n", cfbytesalloc(h2), cfbytesused(h2));
  217.     cfprintf("    Copy hash object to extended memory.\n");
  218.     if(!(h4 = cfcopy("EXTDMEM/hfile", h2))) {
  219.         cfprintf("Failed to copy hash object.\n");
  220.     }
  221.     cftotalloc("EXTDMEM/", &used, &alloc);
  222.     cfprintf("    EXTDMEM bytesalloc=%d bytesused=%d\n", alloc*1000, used*1000);
  223.  
  224.     if(cfcopy("tfile.cff", h1) != NULL) {
  225.         cfprintf("ERROR: No error when copy of sorted directory to tfile.cff\n");
  226.     }
  227. #if 1
  228.     cfprintf("    entrycnt orig tree=%d hash=%d\n", cfentrycnt(h1), cfentrycnt(h2));
  229.     cfprintf("    entrycnt copy tree=%d hash=%d\n", cfentrycnt(h3), cfentrycnt(h4));
  230.     cfprintf("    original size of tree object = %d copied size=%d\n",
  231.         cfbytesalloc(h1), cfbytesalloc(h3));
  232.     cfprintf("    original size of hash object = %d copied size=%d\n",
  233.         cfbytesalloc(h2), cfbytesalloc(h4));
  234. #endif
  235.  
  236.     cfprintf("    Retrieve pushed data from copied objects.\n");
  237.     for(i = 19; i >= 0; --i)
  238.     {
  239.         if(cfpop_data(h3, buf, 32) == ERROR)
  240.             cfprintf("Popdata for hash failed at i=%d\n");
  241.         for(j = 0; j < 16; ++j) {
  242.             if(buf[j] != i) {
  243.                 cfprintf("Data for hash pop failed at i=%d j=%d data=%d\n",i,j,buf[j]);
  244.                 break;
  245.             }
  246.         }
  247.         if(cfpop_data(h4, buf, 32) == ERROR)
  248.             cfprintf("Popdata for tree failed at i=%d\n");
  249.         for(j = 0; j < 16; ++j) {
  250.             if(buf[j] != i) {
  251.                 cfprintf("Pop data for tree failed at i=%d j=%d data=%d\n",i,j,buf[j]);
  252.                 break;
  253.             }
  254.         }
  255.     }
  256.     cflastdupname(h3, "address", 7, &dupname3);
  257.     cflastdupname(h4, "address", 7, &dupname4);
  258.     cfprintf("    Retrieve lots of dupname data from copied objects.\n");
  259.     for(i = 2999; i >= 0; --i)
  260.     {
  261.         if(cfget_dupname(h3, &dupname3, buf, 32) < FOUND) {
  262.             cfprintf("Get dupname data tree failed at i = %d\n", i);
  263.         }
  264.         else {
  265.             for(j = 0; j < 16; ++j) {
  266.                 if(buf[j] != i) {
  267.                     cfprintf("Data for tree wrong at i=%d j=%d data=%d\n", i, j, buf[j]);
  268.                     break;
  269.                 }
  270.             }
  271.         }
  272.         if(cfget_dupname(h4, &dupname4, buf, 32) < FOUND) {
  273.             cfprintf("Get dupname data hash failed at i = %d\n",i);
  274.         }
  275.         else {
  276.             for(j = 0; j < 16; ++j) {
  277.                 if(buf[j] != i) {
  278.                     cfprintf("Data for hash wrong at i=%d j=%d data=%d\n",i, j, buf[j]);
  279.                     break;
  280.                 }
  281.             }
  282.         }
  283.         --dupname3.name;
  284.         --dupname4.name;
  285.     }
  286.     cfclose(h1);
  287.     cfclose(h2);
  288.     cfunlink(h3, NULL);
  289.     cfunlink(h4, NULL);
  290.     cfprintf("  End test2\n");
  291. }
  292. static void
  293. test3()
  294. {
  295. void *h1, *h2;
  296.  
  297.     cfprintf("  Begin test3 -- myfile.cff (retest with permtest)\n");
  298.     if(cfentrycnt("myfile.cff") == ERROR)
  299.     {
  300.         cfprintf("The file 'myfile.cff' does not exist.\nRun 'permtest' to create it.\n");
  301.         return;
  302.     }
  303.  
  304. #if 0
  305. {
  306. long used, alloc;
  307.     cfprintf("    MYFILE.CFF BEFORE COPY\n");
  308.     cftotalloc("myfile.cff", &used, &alloc);
  309.     cfprintf("    myfile.cff alloc=%lu used=%lu\n", alloc, used);
  310.     cfprintf("    entrycnt=%d\n", cfentrycnt("myfile.cff"));
  311.     cfprintbitmaps("myfile.cff");
  312. }
  313. #endif
  314.  
  315.     if((h1 = cfcopy("MEMORY/myfile", "myfile.cff")) != NULL)
  316.     {
  317. #if 0
  318. {
  319. long used, alloc;
  320.         cftotalloc("MEMORY/myfile", &used, &alloc);
  321.         cfprintf("    MEMORY/myfile alloc=%lu used=%lu\n", alloc, used);
  322.         cfprintf("    entrycnt=%d\n", cfentrycnt("MEMORY/myfile"));
  323.         cfprintbitmaps(h1);
  324. }
  325. #endif
  326.         if((h2 = cfcopy("myfile.cff", h1)) != NULL)
  327.         {
  328.             cfunlink(h1, NULL);
  329. #if 0
  330. {
  331. long used, alloc;
  332.             cfprintf("    MYFILE.CFF BEFORE CLOSING\n");
  333.             cftotalloc(h2, &used, &alloc);
  334.             cfprintf("    OPEN myfile.cff alloc=%lu used=%lu\n", alloc, used);
  335.             cfprintf("    entrycnt=%d\n", cfentrycnt("myfile.cff"));
  336.             cfprintbitmaps(h2);
  337. }
  338. #endif
  339.             cfclose(h2);
  340. #if 0
  341. {
  342. long used, alloc;
  343.             cfprintf("    MYFILE.CFF AFTER CLOSING\n");
  344.             cftotalloc("myfile.cff", &used, &alloc);
  345.             cfprintf("    myfile.cff alloc=%lu used=%lu\n", alloc, used);
  346.             cfprintf("    entrycnt=%d\n", cfentrycnt("myfile.cff"));
  347.             cfprintbitmaps("myfile.cff");
  348.  
  349. }
  350. #endif
  351.         } else cfprintf("Failed to copy memory to %s\n", "myfile.cff");
  352.     } else cfprintf("Failed to copy %s to memory.\n", "myfile.cff");
  353.     cfprintf("  End test3\n");
  354. }
  355.  
  356.